package com.yanqu.road.dao.dbV2.serializer;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class DDateTime extends Serializer<LocalDateTime>{

    private static DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public LocalDateTime deserialize(Object object, Field field) {
        if (object instanceof Timestamp) {
            Timestamp t = (Timestamp) object;
            return t.toLocalDateTime();
        }
        return  this.defaultValue();
    }

    @Override
    public LocalDateTime serialize(LocalDateTime date) {
        if (date == null) {
            return this.defaultValue();
        }
        return date;
    }

    @Override
    public String SqlType() {
        return "datetime";
    }

    @Override
    public LocalDateTime defaultValue() {
        return LocalDateTime.parse("2000-01-01 00:00:00", dtf);
    }

    public static DateTimeFormatter getDtf() {
        return dtf;
    }
}
